package config.indoor.nexd.com.nexdmapconfig.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckedTextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import config.indoor.nexd.com.nexdmapconfig.model.Mall;

/**
 * Created by wangxu on 16/2/24.
 */
public class MallAdapter extends RecyclerView.Adapter<MallAdapter.ViewHolder> {

    private List<Mall> malls = new ArrayList<>();
    private Context context;

    public MallAdapter(Context context, List<Mall> malls) {
        this.context = context;
        this.malls.addAll(malls);
    }

    public void addMalls(List<Mall> malls) {
        this.malls.clear();
        this.malls.addAll(malls);
        this.notifyDataSetChanged();
    }


    public Mall getItem(int position) {
        return malls.get(position);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, android.R.layout.simple_list_item_checked, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Mall mall = getItem(position);
        if (mall.isSelected()) {
            holder.checkBox.setChecked(true);
        } else {
            holder.checkBox.setChecked(false);
        }
        holder.checkBox.setText(malls.get(position).getMallName());
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return malls.size();
    }

//    @Override
//    public View getView(int position, View convertView, ViewGroup parent) {
//        ViewHolder viewHolder;
//        if (convertView == null) {
//            viewHolder = new ViewHolder();
//            convertView = View.inflate(context, android.R.layout.simple_list_item_checked, null);
//            viewHolder.checkBox = (CheckedTextView) convertView.findViewById(android.R.id.text1);
//            convertView.setTag(viewHolder);
//        } else {
//            viewHolder = (ViewHolder) convertView.getTag();
//        }
//
//        Mall mall = malls.get(position);
//        if (mall.isSelected()) {
//            viewHolder.checkBox.setChecked(true);
//        } else {
//            viewHolder.checkBox.setChecked(false);
//        }
//        viewHolder.checkBox.setText(malls.get(position).getMallName());
//        return convertView;
//    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(android.R.id.text1)
        CheckedTextView checkBox;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
